package cn.driver.controller;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import cn.driver.domain.User;
import cn.driver.service.UserService;
import cn.driver.utils.MyException;
@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	//登录
	@RequestMapping("/login")
	public Map<Object,Object> login(String userName,String passWord,HttpServletResponse  response){
		Map<Object,Object> map = new HashMap<Object,Object>();
		try{
			if("".equals(userName)||userName==null||passWord==null||"".equals(passWord)){
				map.put("success", false);
				map.put("message", "账号或密码不能为空");
				return map;
			}
			//检验登录信息
			User s=userService.login(userName,passWord);
			String userInfo = s.getId()+"%"+s.getUserName()+"%"+s.getState();
			//登录成功 将用户名放入cookie用于回显 过期时间3个小时
			Cookie cookie = new Cookie("login_cookie",URLEncoder.encode(userInfo));
			cookie.setMaxAge(60*60*3);
			cookie.setPath("/");
			response.addCookie(cookie);
			map.put("success", true);
			map.put("state", s.getState());
		}catch(MyException e){
			e.printStackTrace();
			map.put("success", false);
			map.put("message", e.getMessage());
			return map;
		}catch(Exception e){
			e.printStackTrace();
			map.put("success", false);
		}
		return map;
	}
	//注册
	@RequestMapping("/regist")
	public Map<Object,Object> regist(String userName,String passWord){
		Map<Object,Object> map = new HashMap<Object,Object>();
		try{
			if(userName==null||
					"".equals(userName)||passWord==null||"".equals(passWord)){
				map.put("success", false);
				map.put("message", "注册信息不能为空");
			}else{
				userService.regist(userName,passWord);
				map.put("success", true);
			}
		}catch (MyException e) {
			e.printStackTrace();
			map.put("success", false);
			map.put("message", e.getMessage());
		}catch (Exception e) {
		e.printStackTrace();
		map.put("success", false);
	}
		return map;
	}
	/**
	 * 退出
	 */
	@RequestMapping("/logout")
	public Map<String,Object> logout(HttpServletResponse response){
		Map<String,Object> map = new HashMap<String,Object>();
		try{
 			Cookie clogin = new Cookie("login_cookie","");
 			clogin.setMaxAge(0);
 			clogin.setPath("/");
 			response.addCookie(clogin);
 			map.put("success", true);
		}catch(Exception e){
			e.printStackTrace();
			map.put("success", false);
		}
		return map;
	}
	
	/**
	 * 退出
	 */
	@RequestMapping("/check")
	public String check(String userName){
		try{
			userService.check(userName);
 			
			return "check-stu.html";
		}catch(MyException e){
			e.printStackTrace();
			return "check.html";
		}
	}
	
}
